{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE audio}
{$USE ini}

0000: NOP

script_name "OWDrive"

// By ArtemQa146

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

const
    Check = 31@
    Check_Global = 30@
    gear_ini = 29@
    car_X = 28@
    Check_id = 27@
    key_pad = 26@
    key_1 = 25@
    rel_anim = 24@
end

0AF0: key_pad = read_int_from_ini_file "cleo\Open Window.ini" section "settings" key "Key Gamepad"
0AF0: key_1 = read_int_from_ini_file "cleo\Open Window.ini" section "settings" key "Key"

while true
wait 0
    if
        player.Defined(0)
    then
    gosub @Check_Global
    if
        Check_Global == true
    then
        Check_id = 0
        gear_ini = 0

        0441: 5@ = car 0@ model
        0AD3: 10@v = string_format "%d" 5@
        0AF0: Check_id = get_int_from_ini_file "CLEO\Open Window.ini" section "Car Exception" key 10@v

        0441: 5@ = car 0@ model
        0AD3: 10@v = string_format "%d" 5@
        0AF0: gear_ini = get_int_from_ini_file "CLEO\Gear Change.ini" section "Gear" key 10@v
        
        0085: car_X = 0@ // (int)
        
        rel_anim = true

        repeat
        wait 0
        04ED: load_animation  "DrIVeMod"
        until 04EE:   animation "DrIVeMod" loaded

timera = 0

while true
wait 0
    gosub @Check_Global

        if and
            003B:   car_X == 0@  // (int)
            Check_Global == true
        then
            if
                Check_id == 1
            then
                continue
            end

                    046C: 1@ = car 0@ driver
                    if
                        803B:   not 1@ == $PLAYER_ACTOR  // (int)
                    then
                        continue
                    end

                    if or
                        09BB:   car 0@ has_visible_damage_on_component 2
                        8ABE:   not car 0@ engine_on
                        0E42: is_char_doing_task_id $PLAYER_ACTOR task_id 1022
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_L"
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_R"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_L"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_R"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_TUNE_RADIO"
                    then
                        timera = 5000
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "DRIVE_L"
                        Actor.Animation($PLAYER_ACTOR) == "DRIVE_R"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT2"
                        Actor.Animation($PLAYER_ACTOR) == "NAZADPRO"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATPRO"
                    then
                        timera = 5500
                        continue
                    end

                    if or
                        Actor.Animation($PLAYER_ACTOR) == "sportNAZAD"
                        Actor.Animation($PLAYER_ACTOR) == "sportVOZVRAT"
                        Actor.Animation($PLAYER_ACTOR) == "sportVOZVRAT2"
                        Actor.Animation($PLAYER_ACTOR) == "NAZADTRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATTRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRAT2TRUCK"
                        Actor.Animation($PLAYER_ACTOR) == "NAZAD"
                    then
                        timera = 5500
                        continue
                    end

                    if or
                        Actor.Animation($PLAYER_ACTOR) == "gear_ch"
                        Actor.Animation($PLAYER_ACTOR) == "gear_ch2"
                        Actor.Animation($PLAYER_ACTOR) == "gear_chL"
                        Actor.Animation($PLAYER_ACTOR) == "VOZVRATPRO2"
                        Actor.Animation($PLAYER_ACTOR) == "horn"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIMhide"
                    then
                        timera = 5500
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "horn"
                        Actor.Animation($PLAYER_ACTOR) == "handbrake"
                        Actor.Animation($PLAYER_ACTOR) == "handbrakeL"
                    then
                        timera = 5500
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM1"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM2"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM3"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM4"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM5"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM6"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM7"
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM8"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM9"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM10"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM11"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM12"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM13"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM14"
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM15"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM16"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM17"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM18"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM19"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM20"
                        Actor.Animation($PLAYER_ACTOR) == "AFKANIM21"
                    then
                        timera = 0
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "startgta4LOWG"
                        Actor.Animation($PLAYER_ACTOR) == "startgta4KartG"
                        Actor.Animation($PLAYER_ACTOR) == "startgta4garage"
                        Actor.Animation($PLAYER_ACTOR) == "startgta41"
                        Actor.Animation($PLAYER_ACTOR) == "startgta4LOW"
                    then
                        timera = 0
                        continue
                    end
                    
                    if
                        Check_id == 2
                    then
                        if and
                            rel_anim == true
                            88A7:   not car 0@ componentB 2 opened_or_not_present
                        then
                            gosub @Relax_anim
                        end
                        
                        if
                            timerb > 300
                        then
                            if
                                0A4B:   controls_set_to_joystick
                            then
                                if
                                    00E1:   player 0 pressed_key key_pad
                                then
                                    if
                                        rel_anim == true
                                    then
                                        rel_anim = false
                                    else
                                        rel_anim = true
                                    end
                                    timerb = 0
                                end
                            else
                                if
                                    0AB0:   is_key_pressed key_1
                                then
                                    if
                                        rel_anim == true
                                    then
                                        rel_anim = false
                                    else
                                        rel_anim = true
                                    end
                                    timerb = 0
                                end
                            end
                        end
                        continue
                    end


                    if
                        88A7:   not car 0@ componentB 2 opened_or_not_present
                    then
                        if
                            0AB1: cleo_call @IsWindowOpen params 2 vehicle 0@ door_id 10
                        then
                            
                            if and
                                not Actor.Animation($PLAYER_ACTOR) == "wclose"
                                timerb > 300
                            then
                                1@ = false

                                if
                                    0A4B:   controls_set_to_joystick
                                then
                                    if
                                        00E1:   player 0 pressed_key key_pad
                                    then
                                        1@ = true
                                    end
                                else
                                    if
                                        0AB0:   is_key_pressed key_1
                                    then
                                        1@ = true
                                    end
                                end

                                if
                                    1@ == true
                                then
                                    0605: actor $Player_actor perform_animation "wclose" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    timerb = 0
                                end

                            end
                            
                            if and
                                not Actor.Animation($PLAYER_ACTOR) == "wclose"
                                timerb > 10000
                            then
                                0D59: get_current_weather_to 1@
                                if or
                                    1@ == 8
                                    1@ == 16
                                then
                                    0605: actor $Player_actor perform_animation "wclose" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    timerb = 0
                                else
                                    timerb = 1000
                                end
                            end

                            if
                                Actor.Animation($PLAYER_ACTOR) == "wclose"
                            then
                                0613: 1@ = actor $PLAYER_ACTOR animation "wclose" time
                                if
                                    1@ > 0.9
                                then
                                    0D33: set_car 0@ door 10 window_state 0
                                    continue
                                end
                            end
                            
                            2@ = 20.0
                            gosub @Relax_anim
                            
                        else
                        
                            timera = 4000
                            
                            if and
                                not Actor.Animation($PLAYER_ACTOR) == "wopen"
                                timerb > 300
                            then
                                1@ = false
                                
                                if
                                    0A4B:   controls_set_to_joystick
                                then
                                    if
                                        00E1:   player 0 pressed_key key_pad
                                    then
                                        1@ = true
                                    end
                                else
                                    if
                                        0AB0:   is_key_pressed key_1
                                    then
                                        1@ = true
                                    end
                                end
                                
                                if
                                    1@ == true
                                then
                                    0605: actor $Player_actor perform_animation "wopen" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                    timerb = 0
                                end
                            end
                            
                            if
                                Actor.Animation($PLAYER_ACTOR) == "wopen"
                            then
                                0613: 1@ = actor $PLAYER_ACTOR animation "wopen" time
                                if
                                    1@ > 0.9
                                then
                                    0D33: set_car 0@ door 10 window_state 1
                                    continue
                                end
                            end
                        end
                    end


                else
                    04EF: release_animation "DrIVeMod"
                    break
                end
            end
        end
    end
end

:Check_Global
Check_Global = false
    if and
        Actor.Driving($PLAYER_ACTOR)
        8741:   not actor $PLAYER_ACTOR busted
        not actor.Dead($PLAYER_ACTOR)
        not is_on_cutscene
    then
        if and
            84C8:   not actor $PLAYER_ACTOR driving_flying_vehicle
            847A:   not actor $PLAYER_ACTOR driving_bike
            84A7:   not actor $PLAYER_ACTOR driving_boat
            84A9:   not actor $PLAYER_ACTOR driving_heli
            89AE:   not actor $PLAYER_ACTOR driving_train
        then
            0@ = Actor.CurrentCar($PLAYER_ACTOR)
            0E12: get_vehicle 0@ subclass_to 1@
            if or
                1@ == 1
                1@ == 0
            then
                Check_Global = true
            end
        end
    end
return

:Animation
check = true
    //   
    if or
        Actor.Animation($PLAYER_ACTOR) == "CAR_Lsit"
        Actor.Animation($PLAYER_ACTOR) == "Drive_LO_L"
        Actor.Animation($PLAYER_ACTOR) == "Drive_LO_R"
    then
        check = false
    end
return

:Relax_anim
if and
    not is_char_exiting_any_car $Player_actor
    80E1:   not player 0 pressed_key 15 // / Enter/ F
    not Actor.Animation($PLAYER_ACTOR) == "wopen"
    not Actor.Animation($PLAYER_ACTOR) == "wclose"
    not is_on_cutscene
    not is_on_scripted_cutscene
then
    02E3: 1@ = car 0@ speed
    if or
        gear_ini == 1
        gear_ini == 2
    then
        2@ = 60.0
    else
        2@ = 0.5
    end

    if
        001D:   2@ > 1@  // (int)
    then
        if
            timera > 5000
        then
            
            gosub @Animation
                if
                    Check == true
                then
                    if
                        Actor.Animation($PLAYER_ACTOR) == "drrelaxH"
                    then
                        051E: 1@ = get_current_radio_station
                        if
                            1@ == 12
                        then
                            0614: set_actor $player_actor animation "drrelaxH" progress_to 0.8
                        else
                            0613: 1@ = actor $PLAYER_ACTOR animation "drrelaxH" time
                            if
                                1@ > 0.9
                            then
                                0614: set_actor $player_actor animation "drrelaxH" progress_to 0.1
                            end
                        end
                    else
                        0605: actor $Player_actor perform_animation "drrelaxH" IFP "DrIVeMod" framedelta 2.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                    end
                else
                    if
                        Actor.Animation($PLAYER_ACTOR) == "drrelaxHL"
                    then
                        051E: 1@ = get_current_radio_station
                        if
                            1@ == 12
                        then
                            0614: set_actor $player_actor animation "drrelaxHL" progress_to 0.8
                        else
                            0613: 1@ = actor $PLAYER_ACTOR animation "drrelaxHL" time
                            if
                                1@ > 0.9
                            then
                                0614: set_actor $player_actor animation "drrelaxHL" progress_to 0.1
                            end
                        end
                    else
                        0605: actor $Player_actor perform_animation "drrelaxHL" IFP "DrIVeMod" framedelta 2.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                    end
                end
        end
    else
        timera = 0
    end
else
    timera = 3000
end
return

:IsWindowOpen
if
    056E:   car 0@ defined
then
    0A97: 3@ = vehicle 0@ struct
    0D4E: 5@ = struct 3@ offset 0x18 size 4
    0AA7: call_function_return 0x4C53C0 num_params 2 pop 2 1@ 5@ 4@
    0D4E: 11@ = struct 4@ offset 0x90 size 4
    11@ -= 8
    0085: 4@ = 11@ // (int)
    0A8D: 10@ = read_memory 0x8D608C size 4 virtual_protect 0
    005A: 10@ += 4@
    10@ += 2
    0A8D: 16@ = read_memory 10@ size 2 virtual_protect 0
    08B7:   test 16@ bit 12
    0AB2: cleo_return 0
end
